package com.example.admin.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.admin.mapper.BlogMapper;
import com.example.admin.mapper.CategoryMapper;
import com.example.admin.service.BlogService;
import com.example.xo.pojo.Blog;
import com.example.xo.pojo.Category;
import com.example.xo.vo.BlogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public boolean add(Blog blog) {
        //设置雪花ID
        String id = IdUtil.getSnowflakeNextIdStr();
        blog.setBlogId(id);

        //判断Title是否重复
        Blog blogInDb = findBlogByTitle(blog.getTitle());
        if (blogInDb != null) {
            return false;
        }
        //判断add是否成功    影响字段=1为成功
        return blogMapper.insert(blog)==1;
    }

    @Override
    public boolean deleteById(String id) {
        //通过id查询 查找数据
        Blog blog=findBlogById(id);

        //当查询不到ID时，返回提示
        if (blog==null){
            return false;
        }

        //设置删除时间
        blog.setDeleted(LocalDateTime.now());

        //添加条件
        LambdaQueryWrapper<Blog> wrapper=new LambdaQueryWrapper<>();

        //判断输入的id是否存在
        wrapper.eq(Blog::getBlogId,id);
        int res = blogMapper.update(blog, wrapper);
        return res==1;
    }

    @Override
    public boolean update(Blog blog) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        //判断Id是否存在
        wrapper.eq(Blog::getBlogId,blog.getBlogId());

        //判断是否已被删除
        wrapper.isNull(Blog::getDeleted);

        //添加更新时间
        blog.setUpdated(LocalDateTime.now());

        //判断标题是否重复
        BlogVO blogInDb = findByTitle(blog.getTitle());
        //排除自己本身   加强判断，  标题不空(不重复)   并且   BlogID和自身不同(排除自己)
        if (blogInDb!=null&&!blog.getBlogId().equals(blogInDb.getBlogId())){
            return false;
        }

        //设置作者不可修改
        blog.setAuthor(null);

        int res = blogMapper.update(blog, wrapper);
        return res>0;
    }

    @Override
    public List<Blog> findAllBlog() {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        //创建时间降序排序
        wrapper.orderByDesc(Blog::getCreated);
        //判断是否被软删除
        wrapper.isNull(Blog::getDeleted);
        return blogMapper.selectList(wrapper);
    }

    @Override
    public List<BlogVO> findAll() {
        //设置需要返回的容器VO
        List<BlogVO> vos = new ArrayList<>();
        //查询Blog
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.orderByDesc(Blog::getCreated);
        List<Blog> blogs = blogMapper.selectList(wrapper);
        //根据blog的categoryId来获取分类名
        for (Blog blog : blogs) {
            //通过分类id获取分类对象
            Category category = categoryMapper.selectById(blog.getCategoryId());
            //组装vo
            BlogVO vo = new BlogVO();
            vo.setBlogId(blog.getBlogId());
            vo.setCategoryName(category.getCategoryName());
            vo.setTitle(blog.getTitle());
            vo.setAuthor(blog.getAuthor());
            vo.setCreated(blog.getCreated());

            //将vo加入到容器(vos)中
            vos.add(vo);
        }

        return vos;
    }


    //返回值为BlogVO的findById
    @Override
    public BlogVO findById(String id) {
        //查找Blog
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getBlogId,id);
        Blog blog = blogMapper.selectOne(wrapper);

        //加强---防止输入的title不存在
        if (blog==null){
            return null;
        }

        //为了通过blog中的categoryId查找出categoryName
        Category category = categoryMapper.selectById(blog.getCategoryId());

        //将数据封装进vo中
        BlogVO vo = new BlogVO();
        vo.setBlogId(blog.getBlogId());
        vo.setCategoryName(category.getCategoryName());
        vo.setTitle(blog.getTitle());
        vo.setAuthor(blog.getAuthor());
        vo.setCreated(blog.getCreated());

        return vo;
    }

    //返回值为Blog的findById
    @Override
    public Blog findBlogById(String id) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getBlogId,id);
        wrapper.isNull(Blog::getDeleted);
        return blogMapper.selectOne(wrapper);
    }

    @Override
    public BlogVO findByTitle(String title) {
        //查找Blog
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getTitle,title);
        Blog blog = blogMapper.selectOne(wrapper);

        //加强---防止输入的id不存在
        if (blog==null){
            return null;
        }

        //为了通过blog中的categoryId查找出categoryName
        Category category = categoryMapper.selectById(blog.getCategoryId());

        //将数据封装进vo中
        BlogVO vo = new BlogVO();
        vo.setBlogId(blog.getBlogId());
        vo.setCategoryName(category.getCategoryName());
        vo.setTitle(blog.getTitle());
        vo.setAuthor(blog.getAuthor());
        vo.setCreated(blog.getCreated());

        return vo;
    }

    public Blog findBlogByTitle(String title) {
        //查找Blog
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getTitle,title);
        Blog blog = blogMapper.selectOne(wrapper);

        return blog;
    }

}
